package org.yunshang.leather.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.yunshang.common.excel.utils.ExcelUtil;
import org.yunshang.common.idempotent.annotation.RepeatSubmit;
import org.yunshang.common.log.annotation.Log;
import org.yunshang.common.web.core.BaseController;
import org.yunshang.common.mybatis.core.page.PageQuery;
import org.yunshang.common.core.domain.R;
import org.yunshang.common.core.validate.AddGroup;
import org.yunshang.common.core.validate.EditGroup;
import org.yunshang.common.log.enums.BusinessType;
import org.yunshang.common.mybatis.core.page.TableDataInfo;
import org.yunshang.leather.domain.bo.TLeatherBo;
import org.yunshang.leather.domain.vo.TLeatherVo;
import org.yunshang.leather.service.ITLeatherService;

/**
 * 皮料信息
 *
 * @author Lion Li
 * @date 2024-03-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/leather")
public class TLeatherController extends BaseController {

    private final ITLeatherService tLeatherService;

    /**
     * 查询皮料信息列表
     */
    @SaCheckPermission("business:leather:list")
    @GetMapping("/list")
    public TableDataInfo<TLeatherVo> list(TLeatherBo bo, PageQuery pageQuery) {
        return tLeatherService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出皮料信息列表
     */
    @SaCheckPermission("business:leather:export")
    @Log(title = "皮料信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TLeatherBo bo, HttpServletResponse response) {
        List<TLeatherVo> list = tLeatherService.queryList(bo);
        ExcelUtil.exportExcel(list, "皮料信息", TLeatherVo.class, response);
    }

    /**
     * 获取皮料信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:leather:query")
    @GetMapping("/{id}")
    public R<TLeatherVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tLeatherService.queryById(id));
    }

    /**
     * 新增皮料信息
     */
    @SaCheckPermission("business:leather:add")
    @Log(title = "皮料信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TLeatherBo bo) {
        return toAjax(tLeatherService.insertByBo(bo));
    }

    /**
     * 修改皮料信息
     */
    @SaCheckPermission("business:leather:edit")
    @Log(title = "皮料信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TLeatherBo bo) {
        return toAjax(tLeatherService.updateByBo(bo));
    }

    /**
     * 删除皮料信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:leather:remove")
    @Log(title = "皮料信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tLeatherService.deleteWithValidByIds(List.of(ids), true));
    }
}
